function d=eefc(wt,tm)
    global z2sg;
    z1s=zeros((gsize),H+1);
    //z1sd=zeros((gsize+2),3*gsize,3*gsize);
    z1a=zeros((gsize),H+1,3);
    //z1ad=zeros((gsize+2),3*gsize,3*gsize,3);
    z2s=zeros((gsize),H+1);
    z2a=zeros((gsize),H+1,3);
    z1s(:,H+1)=ones(gsize,1);
    printf("p1\n");
    for n=1:H+1
        for i=gsize:-1:1
            for t=H:-1:1
                //for sa=3*gsize-1:-1:1
                    for j=1:3
                        //check valid action
                        //printf("%d %d %d %d\n",i,t,sa,j);
                        s2=i+j-2;
                        if s2>0 & s2<=gsize //then
                            //z1s(27,81,81)
                            //printf("%d\n",s2);
                            z1a(i,t,j)=exp(rwn(i-1,t-1+tm,j-2,wt))*z1s(s2,t+1);
                        end
                    end
                        z1ar=zeros(3,1);
                        if i==1
                            z1ar(1)=0;
                            z1ar(2)=pact(i,2,2)*z1a(i,t,2)+pact(i,2,3)*z1a(i,t,3);
                            z1ar(3)=pact(i,3,2)*z1a(i,t,2)+pact(i,3,3)*z1a(i,t,3);
                        elseif i==gsize
                            z1ar(1)=pact(i,1,2)*z1a(i,t,2)+pact(i,1,1)*z1a(i,t,1);
                            z1ar(2)=pact(i,2,2)*z1a(i,t,2)+pact(i,2,1)*z1a(i,t,1);
                            z1ar(3)=0;
                        else
                            z1ar(1)=pact(i,1,1)*z1s(i,t,1)+pact(i,1,2)*z1a(i,t,2)+pact(i,1,3)*z1a(i,t,3);
                            z1ar(2)=pact(i,2,1)*z1s(i,t,1)+pact(i,2,2)*z1a(i,t,2)+pact(i,2,3)*z1a(i,t,3);
                            z1ar(3)=pact(i,3,1)*z1s(i,t,1)+pact(i,3,2)*z1a(i,t,2)+pact(i,3,3)*z1a(i,t,3);
                        end
                    //end
                    z1a(i,t,:)=z1ar;
                    z1s(i,t)=sum(z1a(i,t,:));
                    //z1a(i,t,:)=z1a(i,t,:)/z1s(i,t);
                    //printf("%d %d %f\n",i,t,z1s(i,t));
                //end
            end
            //z1s(:,t)=z1s(:,t)/sum(z1s(:,t));
        end
        //z1s(:,3*gsize)=sum(z1a(:,3*gsize,:),3);
    end
    for t=1:H+1
        sm=sum(z1s(:,t));
        if sm>0
            z1s(:,t)=z1s(:,t)/sm;
        end
    end
    printf("%f p2\n",z1s(1,1));
    //z2s(1:min(tm+1,gsize),1)=ones(z2s(1:min(tm+1,gsize),1));
    z2s(:,1)=z2sg(:,tm+1);
//    if tm==0 then
//        z2s(1,1)=1;
//    else
//        z2s(:,1)=ones(z2s(:,1));
//    end
    //z2s(1,1)=1;
    for n=1:H+1
        for i=1:gsize
            for t=1:H
                //for sa=1:3*gsize-1
                    for j=1:3
                        //check valid action
                        s2=i+j-2;
                        if s2>0 & s2<=gsize //then
                            z2a(i,t,j)=exp(rwn(i-1,t-1+tm,j-2,wt))*z2s(i,t);
                        end
                    end
                    z2ar=zeros(3,1);
                    if i==1
                        z2ar(1)=0;
                        z2ar(2)=pact(i,2,2)*z2a(i,t,2)+pact(i,2,3)*z2a(i,t,3);
                        z2ar(3)=pact(i,3,2)*z2a(i,t,2)+pact(i,3,3)*z2a(i,t,3);
                    elseif i==gsize
                        z2ar(1)=pact(i,1,2)*z2a(i,t,2)+pact(i,1,1)*z2a(i,t,1);
                        z2ar(2)=pact(i,2,2)*z2a(i,t,2)+pact(i,2,1)*z2a(i,t,1);
                        z2ar(3)=0;
                    else
                        z2ar(1)=pact(i,1,1)*z2s(i,t,1)+pact(i,1,2)*z2a(i,t,2)+pact(i,1,3)*z2a(i,t,3);
                        z2ar(2)=pact(i,2,1)*z2s(i,t,1)+pact(i,2,2)*z2a(i,t,2)+pact(i,2,3)*z2a(i,t,3);
                        z2ar(3)=pact(i,3,1)*z2s(i,t,1)+pact(i,3,2)*z2a(i,t,2)+pact(i,3,3)*z2a(i,t,3);
                    end
                    //end
                    z2a(i,t,:)=z2ar;
                    //z2a(i,t,:)=z2a(i,t,:)/sum(z2a(i,t,:));
                    if (t>=2 & t<=H) then 
                        if i>=2 & i<gsize then
                            z2s(i,t)=z2a(i-1,t-1,3)+z2a(i+1,t-1,1)+z2a(i,t-1,2);
                        elseif i>=2 then
                            z2s(i,t)=z2a(i-1,t-1,3)+z2a(i,t-1,2);
                        else
                            z2s(i,t)=z2a(i+1,t-1,1)+z2a(i,t-1,2);
                        end   
                    end     
                //end
            end
            //z2s(:,t)=z2s(:,t)/sum(z2s(:,t));
        end
    end
    for t=1:H+1
        sm=sum(z2s(:,t));
        if sm>0
            z2s(:,t)=z2s(:,t)/sm;
        end
    end
    z2sg(:,tm+1:tm+H+1)=z2s(:,:);
    printf("p3 %f\n",z2s(1,1));
    d=zeros(gsize,H+1,3);
    for i=1:gsize
        for t=1:H
            //for sa=1:3*gsize-1
                for j=1:3
                    s2=i+j-2;
                    if s2>0 & s2<=gsize then
                        //printf("%d %d %d %d\n",i,t,sa,j);
                        d(i,t,j)=z2s(i,t)*exp(rwn(i-1,t-1+tm,j-2,wt))*z1s(s2,t+1)/z1s(1,1);
                    end        
                end
            //end
        end
    end
endfunction
function r=rwn(s,t,a,w)
    //y=genf(s+a,t+1,a);
    y=rw(s,t,a,w);
    //disp(w);
    //disp(size(y,1));
    //r=1;
    //wn=w/norm(w,2);
    //r=(y')*w/1003-1;
    r=y;///H-1;//normalise between -1 & 0 or 0 and 1?
endfunction
function r=rw(s,t,a,w)
    y=genf(s+a,t+1,a);
    //disp(w);
    //disp(size(y,1));
    //r=1;
    r=(y')*w;
endfunction
function z=br(w)
    s=0;
    t=0;
    //sa=0;
    z=zeros(3*gsize-1,1);
    while (t<3*gsize-1) 
        a=getract(s,t,w,H);
        s=s+a;
        t=t+1;
        z(t)=a;
        //sa=sa+abs(a);
    end
    //z(t+1)=a;
endfunction
function a=getract(s,t,w,d)
    r=zeros(3,1);
    for j=1:3
        r(j)=rw2(s,t,j-2,w,d);
        //printf("%f ",r(j));
    end
    //printf("\n");
    if s==0 then
        [mr,a]=max(r(2:3));
        a=a+1
    elseif s==gsize-1
        [mr,a]=max(r(1:2));
    else
        [mr,a]=max(r);
        //a=a+1;
    end
    //[mr,a]=max(r);
    a=a-2;
endfunction
function r=rw2(s,t,a,w,d)
    r=0;
    rc=zeros(3,1);
    //printf("%d %d %d %d\n",s,t,a,d)
    if d>0 & s>=0 & s<gsize then

        r=rw(s,t,a,w);
        for j=1:3
            rc(j)=rw2(s+a,t+1,j-2,w,d-1);
        end
        if s==0 then
            mr=max(rc(2:3));
        elseif s==gsize-1
            mr=max(rc(1:2));
        else
            mr=max(rc);
        end
            //printf("%d\n",mr);
        r=r+mr;
    end
    //disp(w);
    //disp(size(y,1));
    //r=1;
    //r=(y')*w;
endfunction

